package com.toidep.preference;

import android.content.Context;
import android.content.SharedPreferences;

import com.toidep.config.Constants;

public class SharePreference {

	public static final String USER_NAME		= "USER_NAME";
	public static final String USER_ID			= "USER_ID";
	public static final String PASSWORD			= "PASSWORD";
	public static final String USER_AVATAR		= "USER_AVATAR";
	public static final String USER_PHONE		= "USER_PHONE";
	public static final String USER_FULL_NAME	= "USER_FULL_NAME";
	public static final String USER_BIRTHDAY	= "USER_BIRTHDAY";
	public static final String USER_GENDER		= "USER_GENDER";
	public static final String USER_EMAIL		= "USER_EMAIL";
	public static final String USER_PROVINCE    = "PROVINCE";
	
	public static final String LOGIN_STATUS		= "LOGIN_STATUS";
	public static final String PAYMENT_APPROVE  = "PAYMENT_APPROVE";
	public static final String ACCESS_TOKEN		= "ACCESS_TOKEN";
	public static final String LAST_TIME_LOGIN	= "LAST_LOGIN";
	
	private static SharePreference instance;
	private Context context;
	
	public static SharePreference getInstance(Context context) {
		if (instance == null) {
			instance = new SharePreference();
			instance.context = context;
		}
		
		return instance;
	}

	// ======================== CORE FUNCTIONS ========================

	/**
	 * Save a long integer to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void putLongValue(String key, long n) {
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putLong(key, n);
		editor.commit();
	}

	/**
	 * Read a long integer to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public long getLongValue(String key) {
		// SmartLog.log(TAG, "Get long integer value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		return pref.getLong(key, 0);
	}

	/**
	 * Save an integer to SharedPreferences
	 * 
	 * @param key
	 * @param n
	 */
	public void putIntValue(String key, int n) {
		// SmartLog.log(TAG, "Set integer value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(key, n);
		editor.commit();
	}

	/**
	 * Read an integer to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public int getIntValue(String key) {
		// SmartLog.log(TAG, "Get integer value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		return pref.getInt(key, -1);
	}

	/**
	 * Save an string to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putStringValue(String key, String s) {
		// SmartLog.log(TAG, "Set string value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(key, s);
		editor.commit();
	}

	/**
	 * Read an string to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public String getStringValue(String key) {
		// SmartLog.log(TAG, "Get string value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		return pref.getString(key, "");
	}

	/**
	 * Read an string to SharedPreferences
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String getStringValue(String key, String defaultValue) {
		// SmartLog.log(TAG, "Get string value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		return pref.getString(key, defaultValue);
	}

	/**
	 * Save an boolean to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putBooleanValue(String key, Boolean b) {
		// SmartLog.log(TAG, "Set boolean value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(key, b);
		editor.commit();
	}

	/**
	 * Read an boolean to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public boolean getBooleanValue(String key, boolean defaultValue) {
		// SmartLog.log(TAG, "Get boolean value");
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		return pref.getBoolean(key, defaultValue);
	}

	/**
	 * Save an float to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putFloatValue(String key, float f) {
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putFloat(key, f);
		editor.commit();
	}

	/**
	 * Read an float to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public float getFloatValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		return pref.getFloat(key, 0.0f);
	}

	// ======================== BASE FUNCTIONS ========================
	
	public void putUserName(String username) {
		putStringValue(USER_NAME, username);
	}

	public String getUserName() {
		return getStringValue(USER_NAME, null);
	}

	public void putUserId(String userId) {
		putStringValue(USER_ID, userId);
	}

	public String getUserId() {
		return getStringValue(USER_ID, null);
	}

	public void putPassword(String pass) {
		putStringValue(PASSWORD, pass);
	}

	public String getPassword() {
		return getStringValue(PASSWORD, null);
	}

	public void putLoginStatus(boolean status) {
		putBooleanValue(LOGIN_STATUS, status);
	}

	public boolean getLoginStatus() {
		return getBooleanValue(LOGIN_STATUS, false);
	}
	public void putPaymentApprove(boolean status) {
		putBooleanValue(PAYMENT_APPROVE, status);
	}

	public boolean getPaymentApprove() {
		return getBooleanValue(PAYMENT_APPROVE, false);
	}
	public void putAccessToken(String accessTOken) {
		putStringValue(ACCESS_TOKEN, accessTOken);
	}

	public String getAccessToken() {
		return getStringValue(ACCESS_TOKEN, null);
	}

	public void putLastTimeLogin(long lastTimeLogin) {
		putLongValue(LAST_TIME_LOGIN, lastTimeLogin);
	}

	public long getLastTimeLogin() {
		return getLongValue(LAST_TIME_LOGIN);
	}

	public void putUserAvatar(String imgUrl) {
		putStringValue(USER_AVATAR, imgUrl);
	}

	public String getUserAvatar() {
		return getStringValue(USER_AVATAR, null);
	}
	
	public void putUserPhone(String userPhone) {
		putStringValue(USER_PHONE, userPhone);
	}

	public String getUserPhone() {
		return getStringValue(USER_PHONE, null);
	}
	
	public void putUserFullName(String fullName) {
		putStringValue(USER_FULL_NAME, fullName);
	}

	public String getUserFullName() {
		return getStringValue(USER_FULL_NAME, null);
	}
	
	public void putUserEmail(String email) {
		putStringValue(USER_EMAIL, email);
	}

	public String getUserEmail() {
		return getStringValue(USER_EMAIL, null);
	}
	
	public void putUserBirthday(String birthday) {
		putStringValue(USER_BIRTHDAY, birthday);
	}

	public String getUserBirthday() {
		return getStringValue(USER_BIRTHDAY, null);
	}
	
	public void putUserGender(int gender) {
		putIntValue(USER_GENDER, gender);
	}

	public int getUserGender() {
		return getIntValue(USER_GENDER);
	}
	
	public void putUserProvince(String province) {
		putStringValue(USER_PROVINCE, province);
	}

	public String getUserProvince() {
		return getStringValue(USER_PROVINCE);
	}
	
	public void clearInfoUser() {
		SharedPreferences pref = context.getSharedPreferences(
				Constants.TOIDEP_ANDROID_PREFERENCES, 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.clear();
		editor.commit();
	}
}
